package br.com.abenildo.visao;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JComboBox;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import br.com.abenildo.controle.Deficiente;
import br.com.abenildo.exception.CPFinvalidoException;
import br.com.abenildo.exception.DeficienteJaCadastradoException;
import br.com.abenildo.exception.DeficienteNaoEncontradoException;
import br.com.abenildo.exception.RepositorioException;
import Faxada.TestarFachada;

import java.awt.Component;

import javax.swing.JScrollBar;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.Collection;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;

public class JInternalFrameListarDefic extends JInternalFrame {
	private JTextField textFieldID;
	private JTable tableDefic;
	private JTextField textFieldCpf;
	Deficiente objdeficiente = new Deficiente();
	JScrollPane scrollPane = new JScrollPane();
	List listaDeficiente = new LinkedList<Deficiente>();
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JInternalFrameListarDefic frame = new JInternalFrameListarDefic();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JInternalFrameListarDefic() {
		setBorder(new LineBorder(new Color(70, 130, 180), 2));
		setToolTipText("");
		setTitle("GRUPO FACABEMFEITO");
		setBounds(100, 100, 450, 300);
		
		JPanel panelBotao = new JPanel();
		
		JPanel panelRegistro = new JPanel();
		
		
		scrollPane.setToolTipText("");
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panelBotao, GroupLayout.PREFERRED_SIZE, 433, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(3)
							.addComponent(panelRegistro, GroupLayout.PREFERRED_SIZE, 428, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(3)
							.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 429, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(panelBotao, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
					.addGap(1)
					.addComponent(panelRegistro, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 122, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		
		tableDefic = new JTable();
		tableDefic.addAncestorListener(new AncestorListener() {
			public void ancestorAdded(AncestorEvent arg0) {
				
			}
			public void ancestorMoved(AncestorEvent arg0) {
			}
			public void ancestorRemoved(AncestorEvent arg0) {
			}
		});
		tableDefic.setToolTipText("");
		tableDefic.setModel(new DefaultTableModel(
			new Object[][] {
				{new Integer(0), new Integer(1), new Integer(2), new Integer(3), new Integer(4)},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
				{null, null, null, null, null},
			},
			new String[] {
				"ID", "CPF", "Nome", "Tipo Defi", "Sexo"
			}
		) {
			Class[] columnTypes = new Class[] {
				Long.class, String.class, String.class, String.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				tableDefic.getValueAt(objdeficiente.getId(), 0);
				return columnTypes[0];
			}
		});
		tableDefic.getColumnModel().getColumn(0).setPreferredWidth(35);
		scrollPane.setViewportView(tableDefic);
		panelRegistro.setLayout(null);
		
		JLabel lblCPF = new JLabel("CPF");
		lblCPF.setBounds(29, 47, 46, 14);
		panelRegistro.add(lblCPF);
		
		textFieldID = new JTextField();
		textFieldID.setBounds(87, 8, 86, 20);
		panelRegistro.add(textFieldID);
		textFieldID.setColumns(10);
		
		textFieldCpf = new JTextField();
		textFieldCpf.setBounds(87, 44, 86, 20);
		panelRegistro.add(textFieldCpf);
		textFieldCpf.setColumns(10);
		
		JLabel lblTipodefic = new JLabel("TipoDefic");
		lblTipodefic.setBounds(228, 14, 61, 14);
		panelRegistro.add(lblTipodefic);
		
		final JComboBox comboBoxTipoDefi = new JComboBox();
		comboBoxTipoDefi.setModel(new DefaultComboBoxModel(new String[] {"\tNormal", "\tVisual"}));
		comboBoxTipoDefi.setBounds(299, 8, 86, 20);
		panelRegistro.add(comboBoxTipoDefi);
		
		JLabel lblSexo = new JLabel("Sexo");
		lblSexo.setBounds(228, 44, 46, 14);
		panelRegistro.add(lblSexo);
		
		final JComboBox comboBoxSexo = new JComboBox();
		comboBoxSexo.setModel(new DefaultComboBoxModel(new String[] {"\tMasculino", "\tFeminino"}));
		comboBoxSexo.setBounds(299, 41, 86, 20);
		panelRegistro.add(comboBoxSexo);
		
		JLabel lblId = new JLabel("ID");
		lblId.setBounds(32, 14, 46, 14);
		panelRegistro.add(lblId);
		panelBotao.setLayout(null);
		
		JButton btnListar = new JButton("Listar");
		btnListar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					TestarFachada.getInstance().listarDeficiente();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnListar.setBackground(new Color(102, 205, 170));
		btnListar.setBounds(20, 10, 89, 23);
		panelBotao.add(btnListar);
		
		JButton btnNewButton_1 = new JButton("Cancelar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
					System.exit(EXIT_ON_CLOSE);;
					
				}
				
			
		});
		btnNewButton_1.setBackground(new Color(102, 205, 170));
		btnNewButton_1.setBounds(317, 10, 89, 23);
		panelBotao.add(btnNewButton_1);
		
		JButton btnExcluir = new JButton("Excluir");
		btnExcluir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) throws NullPointerException {
				try {
					TestarFachada.getInstance().removerDeficiente(objdeficiente.getCpf());
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnExcluir.setBackground(new Color(102, 205, 170));
		btnExcluir.setBounds(119, 10, 89, 23);
		panelBotao.add(btnExcluir);
		
		final JButton btnInserir = new JButton("Inserir");
		btnInserir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try{
					listaDeficiente.add(objdeficiente);
				
					
				//objdeficiente.setId(textFieldID.getText());
				objdeficiente.setCpf(textFieldCpf.getText());
				objdeficiente.setTipo_Defic(comboBoxTipoDefi.getSelectedItem().toString());
				objdeficiente.setSexo(comboBoxSexo.getSelectedItem().toString());
				
				limparCampos();
			}catch(InputMismatchException e1){
				JOptionPane.showMessageDialog(null,"Dados invalidos");
			}catch(Exception e1 ){
				JOptionPane.showMessageDialog(null, e1.getMessage());
			}
				int ID = 0;
				Object valorTabela = tableDefic.getValueAt(objdeficiente.getId(), ID);
				valorTabela.toString();
			}
		});
		btnInserir.setBackground(new Color(102, 205, 170));
		btnInserir.setBounds(218, 10, 89, 23);
		panelBotao.add(btnInserir);
		getContentPane().setLayout(groupLayout);
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{getContentPane(), panelBotao, btnListar, btnNewButton_1, panelRegistro, lblCPF, textFieldID, textFieldCpf, lblTipodefic, comboBoxTipoDefi, lblSexo, comboBoxSexo, scrollPane, tableDefic}));

	}
	
	 public void limparCampos(){
		 textFieldID.setText("");
		 textFieldCpf.setText("");
	 }
}
